# @Time    :2025/4/1 20:37
"""
1）理解类和对象的关系
2）使用面向对象编程来表示汽车和发动机的关系，以及如何组装一台汽车。
# __init__(self, cylinders, horsepower)：构造函数，接收发动机的气缸数
# cylinders 和马力 horsepower，并将其存储为实例属性。
# start(self)：模拟发动机启动，返回启动信息。
# stop(self)：模拟发动机停止，返回停止信息。

"""


class Engine:
    def __init__(self, cylinders, horsepower):
        self.cylinders = cylinders
        self.horsepower = horsepower

    def start(self):
        return "Engine is starting..."

    def stop(self):
        return "Engine is stopping..."


class Car:
    def __init__(self, make, model, year, engine):
        self.make = make
        self.model = model
        self.year = year
        self.engine = engine
        self.speed = 0

    def start(self):
        return self.engine.start()

    def stop(self):
        return self.engine.stop()

    def accelerate(self, amount):
        self.speed += amount
        return f"Car is accelerating. Current speed: {self.speed} mph."

    def brake(self, amount):
        self.speed = max(0, self.speed - amount)
        return f"Car is braking. Current speed: {self.speed} mph."


def main():
    my_engine = Engine(cylinders=6, horsepower=300)
    # 创建一个汽车对象，将发动机对象作为参数传递给汽车对象
    my_car = Car(make="xiaomi", model="su7", year=2024, engine=my_engine)
    print(my_car.start())
    print(my_car.accelerate(30))
    print(my_car.accelerate(20))
    print(my_car.brake(10))
    print(my_car.stop())


if __name__ == "__main__":
    main()
